//
//  RootViewController.m
//  微看
//
//  Created by 吴明磊 on 15/8/21.
//  Copyright © 2015年 wuminglei. All rights reserved.
//

#import "RootViewController.h"
#import "MainController.h"
#import "MineController.h"

@interface RootViewController ()
@property(nonatomic,strong)MainController *mainVc;
@property(nonatomic,strong)MineController *mineVc;
@property(nonatomic,assign)BOOL isPanLeft;
@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    [self hiddenNavBar];
    
    [self setupUI];
    
    [self.mainVc.view addGestureRecognizer:[self addGesture]];
    
    // 添加通知
    [self notificationCenter];
    
}


/// 通知中心
- (void)notificationCenter {
    
    // 接收collectionView滚动
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(receiveNotification:) name:@"myPersonClickNotification" object:nil];
}
/// navBar通知
- (void)receiveNotification:(NSNotification *)notification {
    
    NSDictionary *info = notification.userInfo;
    
    BOOL open = [info[@"isOpen"] intValue];
    
    [self isOpen:open];
}

/// 隐藏系统导航栏
- (void)hiddenNavBar {
    
    self.navigationController.navigationBar.hidden = YES;
}

- (UIGestureRecognizer *)addGesture {

    UIPanGestureRecognizer *gesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panRight:)];
    
    self.isPanLeft = NO;
    
    return gesture;
}

#define targetR 250
- (void)isOpen:(BOOL)isOpen {
    
    
        CGRect translation = isOpen ? [self frameWithOffsetX:targetR] : [UIScreen mainScreen].bounds;
    
    
    if (!isOpen && self.mainVc.view.frame.origin.x == 0) translation = [self frameWithOffsetX:targetR];
    
    if (isOpen && self.mainVc.view.frame.origin.x != 0) translation = [UIScreen mainScreen].bounds;
    
        [UIView animateWithDuration:0.25 animations:^{
            
            self.mainVc.view.frame = translation;
            
        } completion:^(BOOL finished) {
            
            // 动画执行完成判断x为初始位置，设置是否可向左滑动
            self.isPanLeft = self.mainVc.view.frame.origin.x == 0 ?  NO : YES;
        }];
        

    
}

- (void)panRight:(UIPanGestureRecognizer *)pan{


    
    // 获取mainView的偏移量
     CGFloat offsetX = [pan translationInView:self.mainVc.view].x;
    
     // 判断偏移量如果小于0并且在是否可左滑同时成立，否则直接返回
        if (offsetX < 0 && !self.isPanLeft) return;
    
    // 来到这句，说明向右滑动，及时设置isPanLeft 使防止来回拖拽卡住
        self.isPanLeft = YES;
    
    // 在向右滑动中防止猛烈想左滑动出现右边的白边导致抖动
    if (self.mainVc.view.frame.origin.x < 0) {
    
        self.mainVc.view.frame = CGRectMake(0, 0, ScreenSize.width, ScreenSize.height);
        
        self.isPanLeft = NO;
        return;
    }
    
   
    
   // 根据x轴计算当前mainV的frame
        self.mainVc.view.frame = [self frameWithOffsetX:offsetX];
        
        // 复位
        [pan setTranslation:CGPointZero inView:self.view];
    
    
    
    
        if (pan.state == UIGestureRecognizerStateEnded) { // 手指抬起
            CGFloat screenW = [UIScreen mainScreen].bounds.size.width;
            // 定位
            // 当mainV.x > screenW * 0.5 ,定位到右边某个点
            // 当max(mainV.x) < screenW * 0.5 定位到左边某个点
            CGFloat target = 0;
            if (self.mainVc.view.frame.origin.x > screenW * 0.5) {
                target = targetR;

            }
            
            CGFloat offsetX = target - self.mainVc.view.frame.origin.x;
            
        

            
            [UIView animateWithDuration:0.25 animations:^{
                
                self.mainVc.view.frame = [self frameWithOffsetX:offsetX];
            } completion:^(BOOL finished) {
            
                // 动画执行完成判断x为初始位置，设置是否可向左滑动
              self.isPanLeft = self.mainVc.view.frame.origin.x == 0 ?  NO : YES;
            }];
        }
    
    
}


#define kMaxY 100

// 根据x轴偏移量计算出当前mainV的frame
- (CGRect)frameWithOffsetX:(CGFloat)offsetX
{

    
    // 获取屏幕的宽度
    CGFloat screenW = [UIScreen mainScreen].bounds.size.width;
    
    // 获取屏幕的高度
    CGFloat screenH = [UIScreen mainScreen].bounds.size.height;
    
    // 修改红色的frame
    CGRect frame = self.mainVc.view.frame;
    
    
    // 计算当前的x
    CGFloat x = frame.origin.x + offsetX;
    
    // 计算当前的y
    CGFloat y = x * kMaxY / screenW;
    
    if (self.mainVc.view.frame.origin.x < 0) { // 往左移动
        y = -y;
    }
    
    // 获取当前的高度
    CGFloat h = screenH - 2 * y;
    
    // 计算高度的缩放比例
    CGFloat scale = h / screenH;
    
    // 计算当前宽度
    CGFloat w = screenW * scale;
    
    return CGRectMake(x, y, w, h);
}

- (void)setupUI {



    [self.view addSubview:self.mineVc.view];
    [self.view addSubview:self.mainVc.view];

    
}

//MARK: 懒加载
- (MainController *)mainVc {

    if (!_mainVc) {
        _mainVc = [[MainController alloc]init];
    }
    return _mainVc;
}

- (MineController *)mineVc {

    if (!_mineVc) {
        _mineVc = [[MineController alloc]init];
    }
    return _mineVc;
}

@end
